package com.zhkj.txg.module.member.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.adapter.WithdrawListAdapter
import com.zhkj.txg.module.mine.vm.MineViewModel
import kotlinx.android.synthetic.main.activity_list.*
import java.text.SimpleDateFormat
import java.util.*

class MemberWithdrawListActivity : ViewModelActivity<MineViewModel>() {

    companion object {
        private val EXTRA_TYPE = "type"
        private val TYPE_MEMBER = "member"
        private val TYPE_USER = "user"

        fun member(activity: Activity) {
            val intent = Intent(activity, MemberWithdrawListActivity::class.java)
                .putExtra(EXTRA_TYPE, TYPE_MEMBER)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
            activity.startActivity(intent)
        }

        fun user(activity: Activity) {
            val intent = Intent(activity, MemberWithdrawListActivity::class.java)
                .putExtra(EXTRA_TYPE, TYPE_USER)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
            activity.startActivity(intent)
        }
    }

    private val itemAdapter = WithdrawListAdapter()
    private lateinit var tvDate: TextView
    private val datePatten = SimpleDateFormat("yyyy-MM")
    private val dateStrPatten = SimpleDateFormat("yyyy年MM月")

    // 筛选日期的picker
    private val datePicker: TimePickerView by lazy {
        TimePickerBuilder(this) { date, _ ->
            datePicker.dismiss()
            setDate(date)
        }.setType(booleanArrayOf(true, true, false, false, false, false))
            .setTitleText(getString(R.string.date_hint)).build()
    }

    override fun setViewModel(): MineViewModel = createViewModel()
    override val layoutResId: Int = R.layout.activity_list
    override fun statusTitleBar(): View? = titleBar

    override fun initUI() {
        with(refreshLayout) {
            setOnLoadMoreListener {
                if (intent.getStringExtra(EXTRA_TYPE) == TYPE_USER) viewModel.withdrawList(false)
                else viewModel.memberWithdrawList(false)
            }
            setOnRefreshListener {
                if (intent.getStringExtra(EXTRA_TYPE) == TYPE_USER) viewModel.withdrawList(true)
                else viewModel.memberWithdrawList(true)
            }
        }
        with(rvList) {
            layoutManager = LinearLayoutManager(context)
            adapter = itemAdapter
        }
        itemAdapter.setHeaderView(layoutInflater.inflate(R.layout.item_select_date, null).apply {
            tvDate = findViewById(R.id.tvDate)
            tvDate.setOnClickListener {
                datePicker.show()
            }
        })
    }

    override fun initData() {
        titleBar.setTitleTextContent(getString(R.string.withdraw_history))
        setDate(Date())
    }

    override fun initViewModel() {
        viewModel.withdrawListLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 判断加载状态
                    if (!it.loadMore) refreshLayout.finishRefresh()
                    else if (it.hasMore) itemAdapter.loadMoreModule?.loadMoreComplete()
                    else itemAdapter.loadMoreModule?.loadMoreEnd()
                    // 数据填充
                    if (it.loadMore) itemAdapter.addData(it.data)
                    else itemAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    if (viewModel.page > 1) itemAdapter.loadMoreModule?.loadMoreFail()
                    else refreshLayout.finishRefresh(false)
                }
            }
        })
    }

    private fun setDate(date: Date) {
        viewModel.date = datePatten.format(date)
        tvDate.text = dateStrPatten.format(date)
        if (intent.getStringExtra(EXTRA_TYPE) == TYPE_USER) viewModel.withdrawList(true)
        else viewModel.memberWithdrawList(true)
    }
}